var CommonUtil = {
    isEmpty: function(param){
        if(param instanceof Array){
            return param.length == 0; //长度等于0 表示数组为空
        }else if(undefined==param||""==param||null==param){
            return true;
        }else{
            return false;
        }
    },
    isNotEmpty: function (param) {
        return !CommonUtil.isEmpty(param);
    },
    isOneRow: function(param){
        if(CommonUtil.isEmpty(param)){
            return false;
        }
        if(param instanceof Array){
            return param.length ==1;
        }
        return false;
    },
    isNotOneRow : function(param){
        return !CommonUtil.isOneRow(param);
    },
    isSuccess: function(param) {
        if (CommonUtil.isNotEmpty(param) && param.success) {
            return true;
        }
        return false;
    },
    isLogin: function(param){
        if (CommonUtil.isNotEmpty(param) && param.notAuthority) {
            return true;
        }
        return false;
    },
    isEmptyReturn : function(){
        switch(arguments.length){
            case 0: //
                return "";
            case 1:
                if(Array.isArray(arguments[0])){ //function(array)
                    for(var i =0 ; i<arguments[0].length ; i++){
                        var row = arguments[0][i];
                        if(CommonUtil.isNotEmpty(row)){
                            return row;
                        }
                    }
                }
                return "";
            case 2: //function(object,emptyReturn)
                return CommonUtil.isEmpty(arguments[0])?arguments[1]:arguments[0];
            case 3: //function(IF,emptyReturn,notEmptyReturn)
                return arguments[0]?arguments[2]:arguments[1];
            default:
                console.log("参数数量错误")
                return "";
        };
    },
    isNotEmptyReturn : function(param,returnValue){
        if(CommonUtil.isNotEmpty(param)){
            return returnValue;
        }
        return "";
    },
    /**-----------------LayUi 方法区------------------------------------/
     /**
     *
     * @param tableId  数据表格ID
     * @param where 数据表格查询条件an}
     */
    layuiInitSearch: function(tableId,data){
        layui.table.reload(tableId, {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: data.field //当前容器的全部表单字段，名值对形式：{name: value}
        }, 'data');
        return false;
    },
    /**
     * layui table 数据解析接口
     * @param result
     * @returns {{}|{code: *, data: *, count: PaymentItem | number | p.fileLength}}
     */
    layuiParseData: function(result){ //Result 即为原始返回的数据
        if(!result.success){
            return {};
        }
        return {
            "code": result.code, //解析接口状态
            "count": result.data.total, //解析数据长度
            "data": result.data.records //解析数据列表
        }
    },
    /**
     * 跳转修改页面
     * @param url 页面地址
     * @param title 标题
     * @param obj 数据表格对象
     */
    layuiEditPage: function(url,title,width,height,obj){
        var checkStatus = layui.table.checkStatus(obj.config.id);
        var id = undefined;
        if(obj.event=="edit"){
            if(checkStatus.data.length==1){
                id = checkStatus.data[0].id;
            }
            if(!id){
                return ;
            }
        }
        layer.open({
            type: 2,
            title: title, //不显示标题栏
            method:"post",
            area: [width+'px', height+'px'], //自定义文本域宽高
            shade: 0.5,
            offset:'50px',
            id: 'edit', //设定一个id，防止重复弹出
            moveType: 1, //拖拽模式，0或者1
            content: url+ CommonUtil.isNotEmptyReturn(id,"?id="+id),
            yes: function(index, layero){
                layer.getChildFrame('#submit', index).click();
            },
            btn2: function(index, layero){
                layer.close(index);
            }
        });
    },
    layuiDeleteData : function(url,searchButtonId,obj){
        var checkStatus = layui.table.checkStatus(obj.config.id);
        if(checkStatus.data.length==1){
            layui.$.ajax({
                url: url,
                type: "POST",
                data: {"id": checkStatus.data[0].id},
                dataType: "json",
                success: function(result){
                    if(result.success){
                        CommonUtil.layuiSearch(searchButtonId);
                        layer.msg("删除成功");
                    }else{
                        layer.msg(result.msg);
                    }
                },
                error: function () {
                    layer.alert("网络繁忙,请稍候重试....");
                }
            });
        }
    },
    layuiSearch: function(buttonId){
        layui.$("[lay-filter='"+buttonId+"']").click();
    },
    layuiCloseCurrentLayer: function(){
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    },
    layuiParentSearch: function(buttonId){
        if(window.parent && window.parent.CommonUtil && window.parent.CommonUtil.layuiSearch){
            window.parent.CommonUtil.layuiSearch(buttonId);
        }
    },
    layuiEditPageSaveOnSubmit: function(url,fields,searchId){
        var indexShade = layer.load(2, {shade:0.1});
        layui.$.ajax({
            url: url,
            type: "POST",
            data: JSON.stringify(fields),
            async: false,
            dataType: "json",
            contentType:"application/json;charset=UTF-8",
            success: function(result){
                CommonUtil.result(result,searchId)
                layer.close(indexShade);

            },
            error: function () {
                CommonUtil.tipsFail("网络繁忙,请稍候重试....");
                layer.close(indexShade);
            }
        });
        return false;
    },
    /**--------------------日期处理方法-------------------------------------*/
    /**
     * 日期格式化
     * @param {} format
     * @return {}
     */
    dateFormat : function(format) {
        var date = new Date();
        if(!format){
            format = "yyyy-MM-dd HH:mm:ss"
        }
        var date = {
            "M+": date.getMonth() + 1,
            "d+": date.getDate(),
            "h+": date.getHours(),
            "m+": date.getMinutes(),
            "s+": date.getSeconds(),
            "q+": Math.floor((date.getMonth() + 3) / 3),
            "S+": date.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    },
    /**
     * 上一个月开始
     * @type
     */
    dateLastMonth : function (){
        var date = new Date();
        date.setMonth(date.getMonth() -1);
        return date;
    },
    /**
     * 下一个月开始
     * @type
     */
    dateNextMonth : function (){
        var date = new Date();
        date.setMonth(date.getMonth() +1);
        return date;
    },
    /**
     * 获取当天的开始
     */
    dateBeginOfDay : function(){
        var date = new Date();
        date.setHours(0)
        date.setMinutes(0)
        date.setSeconds(0)
        date.setMilliseconds(0)
        return date;
    },
    /**
     * 获取当天的结束
     * @return {}
     */
    dateEndOfDay : function(){
        var date = new Date();
        date.setHours(23)
        date.setMinutes(59)
        date.setSeconds(59)
        date.setMilliseconds(999)
        return date;
    },
    /**
     * 获取当月最后一天日期
     * @type
     */
    dateFirstDayOfMonth : function (){
        var date = new Date();
        date.setDate(1)
        return date;
    },
    /**
     * 获取当月最后一天日期
     * @return {Date}
     */
    dateLastDayOfMonth : function (){
        var date = new Date();
        return new Date(date.getTime()-1000*60*60*24);
    },
    /**
     * 当前系统时间
     * @return {Date}
     */
    dateNow : function (){
        var date = new Date();
        return new Date();
    },
    /**---------------Result 帮助方法-----------------------**/
    result: function(result,searchId){
        if(!result.success){
            layer.msg(result.msg, {icon: 6});
        }else{
            CommonUtil.layuiParentSearch(searchId);
            CommonUtil.layuiCloseCurrentLayer();
        }
    },
    /**-------------------弹出层提示帮助----------------------------**/
    tipsSuccess : function (msg){
        layer.msg(msg, {icon: 6});
    },
    tipsFail : function (msg) {
        layer.msg(msg, {icon: 5});
    },
    tipsWarning : function (msg) {
        layer.msg(msg, {icon: 7});
    }
}
